package com.it.nongchanping.service.impl;

import com.it.nongchanping.entity.News;
import com.it.nongchanping.mapper.NewsMapper;
import com.it.nongchanping.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 新闻服务实现类
 */
@Service
public class NewsServiceImpl implements NewsService {
    
    @Autowired
    private NewsMapper newsMapper;
    
    @Override
    public List<News> getAllNews() {
        return newsMapper.findAll();
    }
    
    @Override
    public News getNewsById(Long id) {
        return newsMapper.findById(id);
    }
    
    @Override
    public List<News> getNewsByType(String type) {
        return newsMapper.findByType(type);
    }
    
    @Override
    public List<News> getPublishedNews() {
        return newsMapper.findByStatus("发布");
    }
    
    @Override
    public List<News> getLatestNews(int limit) {
        return newsMapper.findLatest(limit);
    }
    
    @Override
    public boolean saveNews(News news) {
        news.setCreateTime(LocalDateTime.now());
        news.setUpdateTime(LocalDateTime.now());
        news.setViewCount(0);
        return newsMapper.insert(news) > 0;
    }
    
    @Override
    public boolean updateNews(News news) {
        news.setUpdateTime(LocalDateTime.now());
        return newsMapper.update(news) > 0;
    }
    
    @Override
    public boolean deleteNews(Long id) {
        return newsMapper.deleteById(id) > 0;
    }
    
    @Override
    public void increaseViewCount(Long id) {
        newsMapper.increaseViewCount(id);
    }

    @Override
    public long countAllNews() {
        return newsMapper.countAll();
    }
} 